<template>
  <div v-if="submitHandle" class="idea-inputbox">
    <el-input v-model="submitHandle.curUserIdea" :name="curUserIdea" placeholder="请填写处理意见" type="textarea" :autosize="{ minRows: 3, maxRows: 20}" />
    <SelectGroup :css-style="'padding-top:4px;'">
      <el-select slot="select" v-model="commonIdea" name="commonIdeaList" placeholder="--选择常用意见--" :no-data-text="'暂无常用意见'" :clearable="true" @change="applyCommonIdea">
        <el-option
          v-for="(item,index) in commonIdeaList"
          :key="index"
          :label="item.length>49?(item.substring(0,48)+'..'):item"
          :value="index"
        />
      </el-select>
      <el-button slot="append" plain @click.native="saveCommonIdea">保存常用意见</el-button>
    </SelectGroup>

    <Basicform
      v-if="formTitle=='个人常用意见'"
      :title="formTitle"
      :win-width="850"
      :win-height="500"
      :append-to-body="true"
      :toolbar-btns="busiformBtns"
      :on-basic-form-before-open="onBasicFormBeforeOpen"
      :on-basic-form-submited="onBasicFormSubmited"
    >
      <template v-slot:default="slotProps">
        <busi-form :vo="slotProps?slotProps.vo:formVo" :can-edit="1" :module-info="commonIdeaModuleInfo" />
      </template>
    </Basicform>
  </div>
</template>

<script>

const moment = require('moment')

export default {
    name: 'IdeaInputBox',
    components: {

    },
    props: {
        submitHandle: { type: Object, default: function() { return {} } },
        commonIdeaList: { type: Array, default: function() { return {} } }
    },
    data() {
        return {
            formTitle: '',
            formVo: null,
            busiformBtns: [],
            commonIdea: '',
            commonIdeaModuleInfo: { 'formTemplate': 'dcontent/busiformurl?vwtype=W&url=/usersetting/commonSuggest/form.html', 'addNewUrl': 'usersetting/commonSuggest/addNew', 'insertUrl': 'usersetting/commonSuggest/insert', 'moduleCode': 'commonSuggest' }
        }
    },
    computed: {

    },
    watch: {

    },
    created() {

    },
    beforeDestroy() {

    },
    mounted() {

    },
    methods: {
        applyCommonIdea(val) {
            // if (this.commonIdea === 0) return
            if (val === '') return
            const ideaContent = this.commonIdeaList[this.commonIdea]
            if (this.submitHandle.curUserIdea === undefined || this.submitHandle.curUserIdea === null || this.submitHandle.curUserIdea.trim() === '') {
                this.submitHandle.curUserIdea = ideaContent
            } else {
                this.$confirm('处理意见已存在，是否确认使用常用意见？', '确认操作', { type: 'warning' })
                    .then(() => {
                        this.submitHandle.curUserIdea = ideaContent
                    })
            }
        },
        saveCommonIdea() {
            if (this.submitHandle.curUserIdea === null || this.submitHandle.curUserIdea.trim() === '') {
                this.$alert('请填写处理意见。', '操作提示', { type: 'warning' })
                return
            }
            this.formTitle = '个人常用意见'
            this.$options.components['BusiForm'] = this.$wfEngine.loadBusiform(this.$vnode.context, this.commonIdeaModuleInfo.formTemplate, 'W')
            this.$nextTick(() => {
                this.$bus.emit('openBusiform', this.commonIdeaModuleInfo, null, 1)
            })
        },
        onBasicFormBeforeOpen($scope, vo, defer) {
            console.log($scope.moduleInfo.moduleCode + ' form before open')

            $scope.$set(vo, 'commonIdea', this.submitHandle.curUserIdea)
            $scope.$set(vo, 'updateTime', moment(new Date()).format('YYYY-MM-DD HH:mm:ss'))

            this.formVo = vo
            defer.resolve(true)
        },
        onBasicFormSubmited($scope, vo) {
            console.log($scope.moduleInfo.moduleCode + ' form submited')
            this.formTitle = ''
            this.formVo = null

            this.commonIdeaList.unshift($scope.vo.commonIdea)
        }

    }
}
</script>

<style>

</style>
